// cart.js
var api = require('../../api.js');
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        host:app.globalData.host,
        total_price: 0.00,
        cart_check_all: false,
        cart_list: [],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var page = this;
        page.setData({
            cart_check_all: false,
            show_cart_edit: false,
        });
        page.getCartList();
    },

    getCartList: function () {
        var page = this;
        //wx.showNavigationBarLoading();
        page.setData({
            show_no_data_tip: false,
        });
        app.request({
            url: api.cart.list,
            data:{
              token: app.globalData.token,
              // uid: app.globalData.Wxuser.user_id,
            },
            success: function (res) {
                if (res.code == 1) {
                    page.setData({
                        cart_list: res.data,
                        total_price: 0.00,
                        cart_check_all: false,
                        show_cart_edit: false,
                    });
                }else{
                  page.setData({
                    cart_list: [],
                  });
                }
                page.setData({
                    show_no_data_tip: (page.data.cart_list.length == 0),
                });
                //wx.hideNavigationBarLoading();
                //wx.stopPullDownRefresh();
            }
        });
    },

    cartCheck: function (e) {
        var page = this;
        var index = e.currentTarget.dataset.index;
        var cart_list = page.data.cart_list;
        if (cart_list[index].checked) {
            cart_list[index].checked = false;
        } else {
            cart_list[index].checked = true;
        }
        page.setData({
            cart_list: cart_list,
        });
        page.updateTotalPrice();
    },

    cartCheckAll: function () {
        var page = this;
        var cart_list = page.data.cart_list;
        var checked = false;
        if (page.data.cart_check_all) {
            checked = false;
        } else {
            checked = true;
        }
        for (var i in cart_list) {
            if (!cart_list[i].disabled || page.data.show_cart_edit)
                cart_list[i].checked = checked;
        }
        page.setData({
            cart_check_all: checked,
            cart_list: cart_list,
        });
        page.updateTotalPrice();

    },

    updateTotalPrice: function () {
        var page = this;
        var total_price = 0.00;
        var cart_list = page.data.cart_list;
        for (var i in cart_list) {
            if (cart_list[i].checked)
              total_price += parseFloat(cart_list[i].goods_price) * cart_list[i].goods_num;
        }
        page.setData({
            total_price: total_price.toFixed(2),
        });
    },
    numberAdd: function (e) {
      var index = e.currentTarget.dataset.index;
      var list = this.data.cart_list;
      
      if (index !== "" && index != null) {
          list[parseInt(index)].goods_num++;
          var data = {
            goods_num: list[parseInt(index)].goods_num,
            id: list[parseInt(index)].id,
          };
          this.cartChange(data);
      }
    },
    numberSub: function (e) {
      var index = e.currentTarget.dataset.index;
      var list = this.data.cart_list;
      if (index !== "" && index != null) {
        if (list[parseInt(index)].goods_num > 1) {
          list[parseInt(index)].goods_num--;
          var data = {
            goods_num: list[parseInt(index)].goods_num,
            id: list[parseInt(index)].id,
          };
          this.cartChange(data);
        }
      }
    },
    numberBlur: function (e) {
      var num = e.detail.value;
      var index = e.currentTarget.dataset.index;
      var list = this.data.cart_list;
      
      num = parseInt(num);
      if (isNaN(num))
        num = 1;
      if (num <= 0)
        num = 1;
      
      if (index !== "" && index != null) {
          var data = {
            goods_num: num,
            id: list[parseInt(index)].id,
          };
        this.cartChange(data);
      }
    },

    cartChange:function(data){
      var page = this;
      wx.request({
        url: api.cart.change_num,
        data:data,
        success:function(res){
          if(res.data.code==1){
            wx.hideLoading();
            page.getCartList();
          }else{
            wx.showToast({
              title: res.data.msg,
              image: "/images/icon-warning.png",
              duration: 1500
            });
          }
        },
      })
    },


    cartSubmit: function () {
        var page = this;
        var cart_list = page.data.cart_list;
        var cart_id_list = [];
        for (var i in cart_list) {
            if (cart_list[i].checked)
                cart_id_list.push(cart_list[i].id);
        }
        if (cart_id_list.length == 0) {
            wx.showToast({
              title:"请选择商品",
              image: "/images/icon-warning.png"
            });
            return true;
        }
        wx.navigateTo({
            url: '/pages/order-submit/order-submit?cart_id_list=' + JSON.stringify(cart_id_list),
        });
    },

    cartEdit: function () {
        var page = this;
        var cart_list = page.data.cart_list;
        for (var i in cart_list) {
            cart_list[i].checked = false;
        }
        page.setData({
            cart_list: cart_list,
            show_cart_edit: true,
            cart_check_all: false,
        });
        page.updateTotalPrice();
    },

    cartDone: function () {
        var page = this;
        var cart_list = page.data.cart_list;
        for (var i in cart_list) {
            cart_list[i].checked = false;
        }
        page.setData({
            cart_list: cart_list,
            show_cart_edit: false,
            cart_check_all: false,
        });
        page.updateTotalPrice();
    },

    cartDelete: function () {
        var page = this;
        var cart_list = page.data.cart_list;
        var cart_id_list = [];
        for (var i in cart_list) {
            if (cart_list[i].checked)
                cart_id_list.push(cart_list[i].id);
        }
        if (cart_id_list.length == 0) {
            wx.showToast({
              title:"请选择商品",
              image: "/images/icon-warning.png"
            });
            return true;
        }
        wx.showModal({
            title: "提示",
            content: "确认删除" + cart_id_list.length + "项内容？",
            success: function (res) {
                if (res.cancel)
                    return true;
                wx.showLoading({
                    title: "正在删除",
                    mask: true,
                });
                app.request({
                    url: api.cart.delete,
                    data: {
                        cart_ids: cart_id_list,
                        token: app.globalData.token,
                        // uid: app.globalData.Wxuser.user_id,
                    },
                    success: function (res) {
                        wx.hideLoading();
                        wx.showToast({
                            title: res.msg,
                        });
                        if (res.code == 1) {
                            // page.cartDone();
                            page.getCartList();
                        }
                    }
                });
            }
        });
    },

});